<template>
	<view class="register_login_box"  >
	
		<form @submit="formSubmit" >
		<view class="form-wrap">
			
			<view class="dummy"></view>
			
			<view class="login-title">欢迎登陆智慧社区</view>
			
			<view class="dummy"></view>
			
			<view class="register_login_input_box">
				<image src="../../../static/login/user.png" class="icon_user"></image>
				<input class="register_login_input" maxlength="20" v-model="user_login" placeholder="输入用户名" />
				<image class="delete-img" v-if="user_login.length > 0" @click="clearIcon(0)" src="../../../static/login/xx.png"></image>
			</view>
			
			<view class="dummy"></view>
			
			<view class="register_login_input_box">
				<image src="../../../static/login/pass.png" class="icon_pass"></image>
				<input class="register_login_input" password="true" maxlength="20" v-model="user_pass" placeholder="输入密码" />
				<image class="delete-img" v-if="user_pass.length > 0" @click="clearIcon(1)" src="../../../static/login/xx.png"></image>
			</view>
			
			<view class="dummy"></view>
			
			<view id="submit" :class="{buttonfade : touchid == 'submit'}"
			class="register_login_input_submit"
			 @touchstart="buttonStart" @touchend="buttonEnd"@click="formSubmit">立即登录</view>
			
			<view class="dummy"></view>
		
		</view>
		</form>
		
		<view class="other_login">
			©版权所有 2021-2022 | 湘ICP备14001965号-04
		</view>
			
	</view>
</template>

<script>
import common from '../../../common/common.js';
var serverUrl = common.serverAddr;
export default {
	data() {
		return {
			user_login:'',
			user_pass:'',
			touchid: ''  //点中按钮的id
		};
	},
	methods: {
		
		buttonStart(e){this.touchid = e.target.id},
		buttonEnd(){
			let that = this
			setTimeout(function(){
				that.touchid = ''
			}, 200)
		},
		
		clearIcon(e) {
			if(e == 0){
				this.user_login = ''
			}else{
				this.user_pass = ''
			}
		},
		
		
		// 表单提交 
		formSubmit() {
			let that=this;
			if(!that.user_login){
				that.$uniApi.tipMsg("请输入用户名！")
			    return
			}
			if(!that.user_pass){
				that.$uniApi.tipMsg("请输密码！")
				return
			}

			this.$uniApi.loading('提交中...')
			
			uni.login({
				provider: 'weixin',
				success: ({ code }) => {
					
					that.postLogin(code);
					
		 		}
			});			
		},
	postLogin(code){
		let that = this;
		uni.request({
			url: this.$common.baseUrl + 'api/OauthController/register_login',
			data: { user_login:this.user_login,user_pass:this.user_pass,code:code},
			method: 'POST',
			fail:res=> {
				uni.hideLoading()
				that.$uniApi.tipMsg("连接服务器失败")
			},
			success: res => {
				console.log(res)
				uni.hideLoading()
				let code = res.data.code;
				console.log(res);
				console.log("sssssssssssssss")
				if(code == 0){
					that.$uniApi.tipMsg("用户名或密码错误")
					return
				}else{
					that.$uniApi.tipMsg("登录成功")
					try {
						//保存用户id						
						var cookieString = JSON.stringify(res.cookies[0])
						cookieString = cookieString.split(";")[0];
						var cookie = cookieString.slice(1);
					    uni.setStorageSync('userid', res.data.data);
						uni.setStorageSync('username', res.data.data);
						uni.setStorageSync('cookie', cookie);
					} catch (e) {
						that.$uniApi.tipMsg("用户信息保存失败")
						return
					}
					setTimeout(function(){
						uni.reLaunch({
							url: '/pages/my/my'
						})
					},1500)
				}			
			}
		});
	}
	},
};
</script>

<style>
@import url('./login.css');
</style>
